/****************************************************************************

	COPYRIGHT(C) MAXSI SOFTWARE, JONAS 'SORTIE' TERMANSEN 2008, 2009, 2010

    This file is part of Maxsi Distribution Protocol (exe).

    Maxsi Distribution Protocol (exe) is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Maxsi Distribution Protocol (exe) is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Maxsi Installer. If not, see <http://www.gnu.org/licenses/>.

	*/ #include "MaxsiDistributionProtocolExeContributors.h" /*

	If you modify this file, please enter your name below and provide contact
	information in MaxsiDistributionProtocolExeContributors.h. For more information please see
	MaxsiDistributionExeProtocolContributors.h.
	
	Contributors to this file:

	- Jonas 'Sortie' Termansen
	- [your name here]

	MaxsiDistributionProtocol.exe
	A very simply program that loads the distribution.override file and
	forwards its command line arguments to MaxsiDistributionProtocol.dll in the
	desired distribution.

	Main.cpp
	The whole program.

****************************************************************************/

#include <windows.h>
#include <direct.h>

void CommandLineToArg(char* Command,int* argc, char*** argv)
{
	if (!Command) { return; }

	bool	Quote			=	false;
	bool	Content			=	false;
	size_t	CommandLen		=	strlen(Command);
	int		NumCommands		=	1;
	size_t	I				=	0;

	for (I=0; I < CommandLen; I++)
	{
		if (Command[I] == '"')
		{
			Quote			=	!Quote;
			Content			=	true;
		}
		else if (Command[I] == ' ')
		{
			if (!Quote&&Content)
			{
				Content		=	false;

				NumCommands++;
			}
		}
		else
		{
			Content			=	true;
		}
	}

	*argv = new char*[NumCommands];
	*argc = NumCommands;
	
	size_t StrBegin		=	0;
	size_t StrLen		=	0;
	size_t CommandNum	=	0;
	
	for (I=0; I < CommandLen; I++)
	{
		if (Command[I] == '"')
		{
			Quote = !Quote;
			Content			=	true;
		}
		else if (Command[I] == ' ')
		{
			if ( !Quote && Content)
			{
				Content		=	false;

				StrLen = I - StrBegin;
				if (Command[StrBegin] == '"')
				{
					StrBegin++;
					StrLen--;
				}
				if (Command[StrBegin+StrLen-1] == '"')
				{
					StrLen-=1;
				}
				char* Argument = 0;
				Argument = new char[StrLen+1];
				memcpy(Argument,Command + StrBegin,StrLen);
				memset(Argument+StrLen,0,1);

				(*argv)[CommandNum] = Argument;		

				CommandNum++;
				StrBegin = I+1;
			}
			else if ( !Quote )
			{
				StrBegin++;
			}
		}
		else
		{
			Content			=	true;
		}
	}
	StrLen = I - StrBegin;
	if (Command[StrBegin] == '"')
	{
		StrBegin++;
		StrLen--;
	}
	if (Command[StrBegin+StrLen-1] == '"')
	{
		StrLen-=1;
	}
	char* Argument = 0;
	Argument = new char[StrLen+1];
	memcpy(Argument,Command + StrBegin,StrLen);
	memset(Argument+StrLen,0,1);

	(*argv)[CommandNum] = Argument;
}

char*	GetWorkingDirectory()
{
	char*	CurrentDirectory		=	0;
	char*	CurrentDirectory_TMP	=	_getcwd(NULL,0);

	size_t CurrentDirectory_TMP_Len = strlen(CurrentDirectory_TMP);

	if (CurrentDirectory_TMP[CurrentDirectory_TMP_Len-1] == '\\' ||
		CurrentDirectory_TMP[CurrentDirectory_TMP_Len-1] == '/')
	{
		CurrentDirectory = new char[CurrentDirectory_TMP_Len+1];
		strcpy_s(CurrentDirectory,CurrentDirectory_TMP_Len+1,CurrentDirectory_TMP);
	}
	else
	{
		CurrentDirectory = new char[CurrentDirectory_TMP_Len+2];
		strcpy_s(CurrentDirectory,CurrentDirectory_TMP_Len+1,CurrentDirectory_TMP);
		memset(CurrentDirectory+CurrentDirectory_TMP_Len,'\\',1);
		memset(CurrentDirectory+CurrentDirectory_TMP_Len+1,0,1);
	}

	free(CurrentDirectory_TMP);

	return CurrentDirectory;
}

 void DeleteCommandLineArg(int argc, char** argv)
{
	for ( int I = 0; I < argc; I++ )
	{
		delete[] argv[I];
	}
	delete[] argv;
}


int WINAPI WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow)
{

	char*		CommandLine		=	GetCommandLineA();
	char*		WorkingDir		=	GetWorkingDirectory();

	int			argc			=	0;
	char**		argv			=	0;

	CommandLineToArg(CommandLine,&argc,&argv);

	ShellExecuteA(NULL,NULL,"\"G:\\programmer\\valve\\steam\\SteamApps\\SourceMods\\Maxsi Distribution\\Maxsi\\dev\\MaxsiInstaller.exe\"",argv[1],NULL,SW_SHOW);

	DeleteCommandLineArg(argc,argv);
	delete[]	WorkingDir;


	//MessageBoxA(NULL,"Please run MaxsiDistributionProtocol.exe with command line arguments by using the MaxsiDistribution:// url scheme.","MaxsiDistributionProtocol",0);

	return 0;
}